<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Administrator
 */
class AuxFunction extends WMS_Controller
{

    var $whsCode;
    protected $userId;

    function __construct()
    {
        parent::__construct();
        $gp = gp();
        $userData = $this->session->userdata('users');
        $this->whsCode = $userData['user_main_whs_code'];
        $this->userId = $userData['id'];
        //加载模型
        $this->load->service('/wms/inventory_service');
        $this->load->model('/wms/auxfunction_model');
    }

    /*
    *手动分配库存列表
    */
    public function priority_list($sku='',$pid='')
    {
        //获取数据
        if(!empty($sku) and !empty($pid))
        {
            $data['sku']=$formField['sku']=$sku;
            $data['pid']=$formField['pid']=$pid;
        }else
        {
            $data=$formField = gp();
        }
        if(!empty($formField['sku']) and !empty($formField['pid']))
        {
            //判断当前配件是否是同意报价或者维修中等待配件
            if(!$this->auxfunction_model->selectProductTrack('end_sid in(6,7) and pid="'.trim($formField['pid']).'"',array('add_time')))
            {
               js_goto(getenv("HTTP_REFERER"), '当前sku和pid还没到同意报价的状态，请确认！');
                exit; 
            }
            $where= array('pid'=>trim($formField['pid']),'sku'=>trim($formField['sku']));
            $whereLike = array();
            $datalist = $this->auxfunction_model->selectFirst($this->auxfunction_model->getProductPartsTable(),$where);
            if(!empty($datalist) and !empty($datalist['whs_code']))
            {
                if(!empty($datalist['out_quantity']))
                {
                    js_goto(getenv("HTTP_REFERER"), '抱歉，此产品已出库！');
                    exit;
                }
                if(!empty($datalist['actual_quantity']))
                {
                    js_goto(getenv("HTTP_REFERER"), '抱歉，此产品已分配库存！');
                    exit;
                }
                $datalist['on_hand'] = $this->inventory_service->get_on_hand(trim($formField['sku']),$datalist['whs_code']);
                $datalist['available'] = $this->inventory_service->get_available(trim($formField['sku']),$datalist['whs_code']);
                $datalist['on_order'] = $datalist['on_hand']-$datalist['available'];
                //根据仓库物料号获取当前等待配件的pid
                // echo $datalist['whs_code'];
                $whsSkulist = $this->auxfunction_model->selectParts(trim($datalist['whs_code']),trim($formField['sku']));
                //获取当前仓库的库存情况
                // 重构数据
                foreach ($whsSkulist as $key => &$vary) 
                {
                    $vary['priority_name'] = ($vary['priority']==1)?'优先':'普通';
                }
                unset($vary);
                $data['whsSkulist'] = $whsSkulist;
                $data['datalist'] = $datalist;
                // echo '<pre>';
                // var_dump($data);
                // exit;
            }else
            {
                js_goto(getenv("HTTP_REFERER"), '当前sku和pid对应的仓库为空，请确认！');
                exit;
            }
        }else if(isset($formField['submits']))
        {
            js_goto(getenv("HTTP_REFERER"), '请输入PID后再查询！');
            exit;
        }
        $this->load->view('defaults/wms/priority_list', $data);
        $this->load->view('defaults/common/footer', $data);
    }

    /*
    *一键优先
    */
    public function onePriority($id,$sku,$pid)
    {
        $this->auxfunction_model->update($this->auxfunction_model->getProductPartsTable(),array('priority'=>'1'),array('id'=>$id));
        $this->priority_list($sku,$pid);
    }

     /*
    *仓库列表
    */
    public function warehouse_list()
    {
        //获取表单数据
        $data = $formField = gp();
        $data['class'] = __CLASS__;
        $data['func'] = __FUNCTION__;
        $where= '';
        $whereLike = array();
        //查询参数组装
        $fieldsList = $this->db->list_fields($this->auxfunction_model->getWarehouseListTable());
        //需要模糊查询的字段
        $likeAry = array();
        $searchStr = 'wms/AuxFunction/warehouse_list/?';
        foreach ($formField as $key => $val) 
        {
            if ($val==='' || !in_array($key, $fieldsList)) continue;
            $val = trim($val);
            if (in_array($key, $likeAry)) 
            {
                
                $whereLike[$key] = $val;
            }else
            {
                $where[$key] = $val;
            }
            $searchStr .= $key . '=' . $val . '&';
        }
        $searchStr = rtrim($searchStr, '&');
        // var_dump($searchStr);
        //分页
        $this->load->library('pagination');
        $config['page_query_string'] = TRUE;
        $base_url = blang_url($searchStr);
        $config['base_url'] = $base_url;
        $config['total_rows'] = $this->auxfunction_model->countRows($this->auxfunction_model->getWarehouseListTable(),$where, '', $whereLike);
        $config['uri_segment'] = config_item('enable_lang') ? 5 : 4;
        $config['per_page'] = 20;
        $data['page'] = isset($data['page'])?$data['page']:1;
        $page = max(1, intval($data['page']));
        $slimit = ($page - 1) * $config['per_page'];
        $this->pagination->initialize($config);
        $data['pageShow'] = $this->pagination->create_links();
        //查询
        $datalist = $this->auxfunction_model->select($this->auxfunction_model->getWarehouseListTable(), $slimit, $config['per_page'], '', $where, '', $whereLike);
        //重构数据
        // foreach ($datalist as $key => &$vary) 
        // {
            
        // }
        unset($vary);
        $data['dataList'] = $datalist;
        $data['whsCodeList'] = $this->auxfunction_model->selectAll($this->auxfunction_model->getWarehouseListTable());
        $data['pointList'] = $this->auxfunction_model->selectAll($this->auxfunction_model->getServiceCenterTable());
        $data['platformNumList'] = $this->auxfunction_model->customersList('type in(1,2)');
        $data['whsTypeList'] = array('merchant'=>'代管仓','maintain'=>'维保仓');
        $data['sapWhsCode'] = array('W001'=>'T01','W005'=>'T03','W006'=>'T02','W014'=>'T01','W033'=>'T05','W032'=>'T06','W049'=>'T07');
        //导出数据
        $this->load->view('defaults/wms/warehouse_list', $data);
        $this->load->view('defaults/common/footer', $data);
    }

    /*
    *修改仓库状态
    */
    public function setActive($whs_code,$active)
    {
        $this->auxfunction_model->update($this->auxfunction_model->getWarehouseListTable(),array('active'=>trim($active)),array('whs_code'=>trim($whs_code)));
        redirect(blang_url('wms/AuxFunction/warehouse_list'));
    }

    /*
    *添加仓库
    */
    public function addWarehouse()
    {
        $formdata = gp();
        //检查当前仓库是否已经存在
        if(empty($formdata['whs_code']) or empty($formdata['whs_name']))
        {
            js_goto(getenv("HTTP_REFERER"), $formdata['whs_code'].' 仓库代码或仓库名称不能为空！');
            exit;
        }
        if($this->auxfunction_model->checkWhsCode($formdata['whs_code']))
        {
            js_goto(getenv("HTTP_REFERER"), $formdata['whs_code'].' 仓库已经存在，请确认！');
            exit;
        }else
        {
            //构造数据
            $fieldsList = $this->db->list_fields($this->auxfunction_model->getWarehouseListTable());
            $fieldsList[] = 'sap_whs_code';
            $insertArr = array();
            foreach ($formdata as $key => $value) 
            {
                $value = trim($value);
                if(in_array($key,$fieldsList))
                {
                    if($key == 'sap_whs_code')
                    {
                        $insertArr['shop_id'] = $value;
                    }else
                    {
                        $insertArr[$key] = $value;
                    }
                }
            }
            if($insertArr['whs_type'] == 'maintain')
            {
                $insertArr['platform_num'] = 0;
                $insertArr['whs_code_s'] = $insertArr['whs_code'];
            }else
            {
                $insertArr['whs_code_s'] = $insertArr['shop_id'].'-VNE';
            }
            $insertArr['whs_dsc'] = $insertArr['whs_name'];
            $insertArr['merchant_num'] = $insertArr['platform_num'];
            $insertArr['created_date'] = time();
            $insertArr['creator_id'] = $this->userId;
            $insertArr['warranty_use'] = 1;
            $insertArr['no_warranty_use'] = 1;
            $insertArr['sold'] = 1;
            $this->auxfunction_model->insert($this->auxfunction_model->getWarehouseListTable(),$insertArr);
            redirect(blang_url('wms/AuxFunction/warehouse_list'));
        }
    }

    /*
    *仓库用户列表
    */
    public function warehouse_user_list()
    {
        //获取表单数据
        $data = $formField = gp();
        $data['class'] = __CLASS__;
        $data['func'] = __FUNCTION__;
        $where= array('wb_type'=>0,'is_open'=>1);
        $whereLike = array('role_id'=>'8');
        //查询参数组装
        $fieldsList = $this->db->list_fields($this->auxfunction_model->getUserTable());
        //需要模糊查询的字段
        $likeAry = array('account','name');
        $searchStr = 'wms/AuxFunction/warehouse_user_list/?';
        foreach ($formField as $key => $val) 
        {
            if ($val==='' || !in_array($key, $fieldsList)) continue;
            $val = trim($val);
            if (in_array($key, $likeAry)) 
            {
                
                $whereLike[$key] = $val;
            }else
            {
                $where[$key] = $val;
            }
            $searchStr .= $key . '=' . $val . '&';
        }
        $searchStr = rtrim($searchStr, '&');
        // var_dump($searchStr);
        //分页
        $this->load->library('pagination');
        $config['page_query_string'] = TRUE;
        $base_url = blang_url($searchStr);
        $config['base_url'] = $base_url;
        $config['total_rows'] = $this->auxfunction_model->countRows($this->auxfunction_model->getUserTable(),$where, '', $whereLike);
        $config['uri_segment'] = config_item('enable_lang') ? 5 : 4;
        $config['per_page'] = 20;
        $data['page'] = isset($data['page'])?$data['page']:1;
        $page = max(1, intval($data['page']));
        $slimit = ($page - 1) * $config['per_page'];
        $this->pagination->initialize($config);
        $data['pageShow'] = $this->pagination->create_links();
        //查询
        $datalist = $this->auxfunction_model->select($this->auxfunction_model->getUserTable(), $slimit, $config['per_page'], '', $where, '', $whereLike);
        //重构数据
        foreach ($datalist as $key => &$vary) 
        {
            $vary['role_name'] = $this->auxfunction_model->getRoleName('id in('.trim($vary['role_id'],',').')');
        }
        unset($vary);
        $data['dataList'] = $datalist;
        //获取所有仓库
        $data['whsArr'] = $this->auxfunction_model->getWhsList();
        $this->load->view('defaults/wms/warehouse_user_list', $data);
        $this->load->view('defaults/common/footer', $data);
    }

    /*
    *仓库权限设置
    */
    public function setWhsPermission()
    {
        $formdata = gp();
        $user_id = trim($formdata['user_id']);
        //获取要设置用用户已经存在的权限
        $whsList = $this->auxfunction_model->getUserWhsInfo(array('user_id'=>$user_id),1);
        $whsList = explode(',',$whsList[0]['whs_code']);
        //添加
        foreach ($formdata['whs_code'] as $val)
        {
            if(!in_array($val,$whsList))
            {
                $this->auxfunction_model->insert($this->auxfunction_model->getUserLinkWhsTable(),array('whs_code'=>$val,'user_id'=>$user_id));
            }
        }

        //删除
        foreach ($whsList as  $val) 
        {
            if(!in_array($val,$formdata['whs_code']))
            {
                $this->auxfunction_model->delete($this->auxfunction_model->getUserLinkWhsTable(),array('whs_code'=>$val,'user_id'=>$user_id));
            }
        }

        redirect(blang_url('wms/AuxFunction/warehouse_user_list'));
    }
}

?>
